package com.imooc.framework.atomic;

import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanDemo implements Runnable {

    private static AtomicBoolean atomicBoolean = new AtomicBoolean();

    @Override
    public void run() {
        if(atomicBoolean.compareAndSet(false,true)){
            System.out.println(Thread.currentThread().getName() + " init success");
        } else {
            System.out.println(Thread.currentThread().getName() + " cas 失败");
        }
    }

    public static void main(String[] args) {
        final AtomicBooleanDemo atomicBooleanDemo = new AtomicBooleanDemo();
        for (int i = 0; i < 100; i++) {
            new Thread(atomicBooleanDemo).start();
        }
    }
}
